﻿/**********************************************
 * 功能名称：成本动因数据锁定
编写人：  杨莹
建立时间： 杨 2009年8月20日
说明：
对所有动因数据，进行锁定，不允许编辑。
 * 并对个别的作业中心进行锁定。
 **************************************************************/
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using Hisun.UserControl;
using Hisun.Controllers;
using System.Text;
using System.Collections.Generic;
namespace Hisun.View.CostDriver
{
    public partial class DriverDataLock : System.Web.UI.Page
    {
        HisunDBDataContext db = new HisunDBDataContext();
        public string alter;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {


                MyDataBind();
            }
        }

        private void MyDataBind()
        {
            string MyAp = Application["accountPeriod"].ToString();

            string MyCenterName = TextBox_CenterName.Text;

            var MyCenterList = ((from a in db.TABC_JS_CostCenter
                               join b in
                                   (from k in db.TABC_JS_CostCenterPathDriver
                                    group k by new { k.作业中心ID, k.会计期间ID, k.锁定 } into g
                                    select new
                                    {
                                        g.Key.作业中心ID,
                                        g.Key.会计期间ID,
                                        g.Key.锁定
                                    })
                               on a.作业中心ID equals b.作业中心ID
                               where b.会计期间ID == MyAp && a.作业中心名称.Contains(MyCenterName)
                               orderby a.作业中心名称

                               select new
                               {
                                   作业中心ID = a.作业中心ID,
                                   作业中心名称 =a.作业中心名称,
                                   锁定 = b.锁定
                               }).Concat(from a in db.TABC_JS_CostCenter
                                         join b in
                                             (from k in db.TABC_JS_ProductCostDriver
                                              group k by new { k.作业中心ID, k.会计期间ID, k.锁定 } into g
                                              select new
                                              {
                                                  g.Key.作业中心ID,
                                                  g.Key.会计期间ID,
                                                  g.Key.锁定
                                              })
                                         on a.作业中心ID equals b.作业中心ID
                                         where b.会计期间ID == MyAp && a.作业中心名称.Contains(MyCenterName)
                                         orderby a.作业中心名称

                                         select new
                                         {
                                             作业中心ID = a.作业中心ID,
                                             作业中心名称 = a.作业中心名称,
                                             锁定 = b.锁定
                                         })).Distinct();
            if (MyCenterList.Count() > 0)
            {
                GridView_LockQuery.DataSource = MyCenterList;
                GridView_LockQuery.DataBind();
                Panel_Main.Visible = true;
            }
            else
            {

                Panel_Main.Visible = false;

            }
        }

        protected void Button_Query1_Click(object sender, EventArgs e)
        {
            MyDataBind();
        }

        protected void Button_AllSelect_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < GridView_LockQuery.Rows.Count; i++)
            {
                GridViewRow myrows = GridView_LockQuery.Rows[i];
                ((CheckBox)myrows.Cells[2].FindControl("CheckBox_Lock")).Checked = true;

            }
        }

        protected void Button_UnSelect_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < GridView_LockQuery.Rows.Count; i++)
            {
                GridViewRow myrows = GridView_LockQuery.Rows[i];
                ((CheckBox)myrows.Cells[2].FindControl("CheckBox_Lock")).Checked = false;

            }
        }

        protected void Button_Save_Click(object sender, EventArgs e)
        {
            string MyAp = Application["accountPeriod"].ToString();



            try
            {
                for (int i = 0; i < GridView_LockQuery.Rows.Count; i++)
                {
                    GridViewRow myrows = GridView_LockQuery.Rows[i];


                    bool MyChecked = ((CheckBox)myrows.Cells[2].FindControl("CheckBox_Lock")).Checked;
                    int MyCenterID = Convert.ToInt32(myrows.Cells[0].Text);

                    var isExistRows1 = from a in db.TABC_JS_CostCenterPathDriver
                                      where a.作业中心ID == MyCenterID && a.会计期间ID == MyAp
                                      select a;

                    if (isExistRows1.Count() > 0)
                    {
                        foreach (var EditRow1 in isExistRows1)
                        {
                          //  //当锁定标志不是当前设置值的时候，更新数据项。
                           // if (EditRow1.锁定 != MyChecked)
                           // {
                                EditRow1.锁定 = MyChecked;
                                db.SubmitChanges();
                          //  }
                        }


                    }


                    var isExistRows2 = from a in db.TABC_JS_ProductCostDriver
                                      where a.作业中心ID == MyCenterID && a.会计期间ID == MyAp
                                      select a;

                    if (isExistRows2.Count() > 0)
                    {
                        foreach (var EditRow2 in isExistRows2)
                        {
                            //当锁定标志不是当前设置值的时候，更新数据项。
                          //  if (EditRow2.锁定 != MyChecked)
                          //  {
                                EditRow2.锁定 = MyChecked;
                                db.SubmitChanges();
                          //  }
                        }


                    }

                }




                alter = "<script>alert('数据更新成功');</script>";

                MyDataBind();
            }
            catch
            {
                alter = "<script>alert('数据更新失败');</script>";
            }

        }
    }
}
